package cn.hy.limiter;

import com.google.common.util.concurrent.RateLimiter;

import java.time.Duration;

/**
 * @author zhaomu
 */
public class GuavaLimiterDemo {

    public static void main(String[] args) {

        RateLimiter smoothBursty = RateLimiter.create(5); // smoothBursty

        /// RateLimiter 允许预占未来的令牌，比如，每秒产生 5 个 permits，
        // 我们可以单次请求 100 个 permits，这样，紧接着的下一个请求需要等待大概 20 秒才能获取到 permits。
        // double acquire = smoothBursty.acquire(100);

       // smoothBursty.acquire(1); // wait 20 s


        RateLimiter smoothWarmingUp = RateLimiter.create(5, Duration.ofSeconds(3));


        for (int i = 0; i < 10; i++) {
            double acquire = smoothWarmingUp.acquire(3);
            System.out.println(acquire);
        }



    }
}
